// Simple scroll sync utility for horizontal scroll syncing between containers

const groupToElements = new Map();
const scheduled = new Map(); // group -> { sourceEl, frameId }

export function registerScrollElement(group, el) {
  if (!group || !el) return () => {};
  let set = groupToElements.get(group);
  if (!set) {
    set = new Set();
    groupToElements.set(group, set);
  }
  set.add(el);
  return () => {
    const s = groupToElements.get(group);
    if (s) {
      s.delete(el);
      if (s.size === 0) groupToElements.delete(group);
    }
  };
}

export function syncHorizontalScroll(group, sourceEl) {
  const set = groupToElements.get(group);
  if (!set) return;
  const prev = scheduled.get(group);
  if (prev && prev.frameId) cancelAnimationFrame(prev.frameId);
  const frameId = requestAnimationFrame(() => {
    for (const el of set) {
      if (el === sourceEl) continue;
      try { el.scrollLeft = sourceEl.scrollLeft; } catch {}
    }
    scheduled.delete(group);
  });
  scheduled.set(group, { sourceEl, frameId });
}


